﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Zvezda.FSO2.DB;
namespace FSO
{
    public partial class UserControlSearchInvoiceIn : UserControl, InterfaceSearchControl
    {
        private bool _isNotFill = true;

        private bool _openOnDblClick = false;

        
        public UserControlSearchInvoiceIn()
        {
            InitializeComponent();

            radGridViewList.MultiSelect = true;

        }

        private void UserControlSearchInvoiceIn_Load(object sender, EventArgs e)
        {
            if (_isNotFill)
            {
                vieW_SEARCH_INVOICE_INTableAdapter1.Fill(dataSetFSO1.VIEW_SEARCH_INVOICE_IN);
                InitializeGridView();
            }         
        }

        private void InitializeGridView()
        {
            GridViewTextBoxColumn colNumber = new GridViewTextBoxColumn("NUMBER", dataSetFSO1.VIEW_SEARCH_INVOICE_IN.NUMBERColumn.ColumnName);
            colNumber.HeaderText = "№";

            GridViewDateTimeColumn colDate = new GridViewDateTimeColumn("DATE", dataSetFSO1.VIEW_SEARCH_INVOICE_IN.DATEColumn.ColumnName);
            colDate.HeaderText = "Дата";
            colDate.FormatString = "{0:d}";

            GridViewTextBoxColumn colRegNum = new GridViewTextBoxColumn("CODE", dataSetFSO1.VIEW_SEARCH_INVOICE_IN.REG_NUMBERColumn.ColumnName);
            colRegNum.HeaderText = "Рег. №";

            GridViewDecimalColumn colSumCur = new GridViewDecimalColumn("SUM", dataSetFSO1.VIEW_SEARCH_INVOICE_IN.SUMColumn.ColumnName);
            colSumCur.HeaderText = "Сумма(вал.)";
            colSumCur.Minimum = 0;
            colSumCur.FormatString = "{0:N2}";

            GridViewTextBoxColumn colCur = new GridViewTextBoxColumn("CURRENCY", dataSetFSO1.VIEW_SEARCH_INVOICE_IN.SHORT_NAME_CURRENCYColumn.ColumnName);
            colCur.HeaderText = "Валюта";

            GridViewTextBoxColumn colOrg = new GridViewTextBoxColumn("ORGANIZATION", dataSetFSO1.VIEW_SEARCH_INVOICE_IN.SHORT_NAME_ORGColumn.ColumnName);
            colOrg.HeaderText = "Организация";

            radGridViewList.MasterTemplate.Columns.Clear();
            radGridViewList.MasterTemplate.Columns.Add(colRegNum);
            radGridViewList.MasterTemplate.Columns.Add(colOrg);
            radGridViewList.MasterTemplate.Columns.Add(colNumber);
            radGridViewList.MasterTemplate.Columns.Add(colDate);           
            radGridViewList.MasterTemplate.Columns.Add(colSumCur);
            radGridViewList.MasterTemplate.Columns.Add(colCur);

            radGridViewList.DataSource = dataSetFSO1.VIEW_SEARCH_INVOICE_IN;
        }



        #region InterfaceSearchControl Members

        public bool OpenOnDoubleClick
        {
            get
            {
                return _openOnDblClick;
            }
            set
            {
                _openOnDblClick = value;
            }
        }

        public bool EnableFiltering
        {
            get
            {
                return radGridViewList.EnableFiltering;
            }
            set
            {
                radGridViewList.EnableFiltering = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                DataSetFSO.VIEW_SEARCH_INVOICE_INRow dr;
                DataRow returnRow;
                if (radGridViewList.SelectedRows.Count > 0)
                {
                    dr = (DataSetFSO.VIEW_SEARCH_INVOICE_INRow)(((DataRowView)(radGridViewList.SelectedRows.First()).DataBoundItem)).Row;
                    returnRow = invoicE_INTableAdapter1.GetDataByID(dr.ID).FindByID(dr.ID);
                }
                else
                {
                    returnRow = null;
                }
                return returnRow;
            }
        }

        public DataSet OwnerDataSet
        {
            get { return dataSetFSO1; }
            set { _isNotFill = false; dataSetFSO1 = (DataSetFSO)value; InitializeGridView(); }
        }

        public GridViewSelectedRowsCollection SelectedRows
        {
            get
            {
                return radGridViewList.SelectedRows;
            }
        }

        public void RefreshList()
        {
            vieW_SEARCH_INVOICE_INTableAdapter1.Fill(dataSetFSO1.VIEW_SEARCH_INVOICE_IN);
        }
        #endregion

        private void radGridViewList_CellDoubleClick(object sender, GridViewCellEventArgs e)
        {
            if (this.SelectedRow != null && _openOnDblClick)
            {

                FormInvoiceIn formInvIn = new FormInvoiceIn((DataSetFSO.INVOICE_INRow)this.SelectedRow);
                if (!FormHelper.HasOpenedForm(formInvIn.FormName))
                {
                    formInvIn.MdiParent = FormHelper.MainForm;
                    formInvIn.Show();
                    formInvIn.Select();
                }

            }
        }
    }
}
